package com.bai.localshop.controller.admin;

import com.bai.localshop.exception.BaseException;
import com.bai.localshop.pojo.dto.VoucherQueryDTO;
import com.bai.localshop.pojo.dto.VoucherSaveDTO;
import com.bai.localshop.pojo.dto.VoucherUpdateDTO;
import com.bai.localshop.pojo.dto.VoucherUpdateStatusDTO;
import com.bai.localshop.pojo.vo.AdminVoucherPageVO;
import com.bai.localshop.pojo.vo.VoucherVO;
import com.bai.localshop.result.Result;
import com.bai.localshop.service.VoucherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 管理端代金券控制器
 */
@RestController("adminVoucherController")
@RequestMapping("/api/admin/vouchers")
@RequiredArgsConstructor
@Slf4j
public class VoucherController {

    private final VoucherService voucherService;

    /**
     * 分页查询代金券
     */
    @GetMapping("/page")
    public Result<IPage<AdminVoucherPageVO>> pageQuery(VoucherQueryDTO requestParam) {
        return Result.success(voucherService.pageQuery(requestParam, AdminVoucherPageVO.class));
    }

    /**
     * 根据ID查询代金券
     */
    @GetMapping("/{id}")
    public Result<VoucherVO> getById(@PathVariable Long id) {
        VoucherVO voucher = voucherService.getById(id);
        if (voucher == null) {
            throw new BaseException("代金券不存在");
        }
        return Result.success(voucher);
    }

    /**
     * 新增代金券
     */
    @PostMapping
    public Result<String> save(@Valid @RequestBody VoucherSaveDTO requestParam) {
        voucherService.save(requestParam);
        return Result.success("新增成功");
    }

    /**
     * 修改代金券
     */
    @PutMapping
    public Result<String> update(@Valid @RequestBody VoucherUpdateDTO requestParam) {
        voucherService.update(requestParam);
        return Result.success("修改成功");
    }

    /**
     * 删除代金券
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteById(@PathVariable Long id) {
        voucherService.deleteById(id);
        return Result.success("删除成功");
    }

    /**
     * 上架下架代金券
     */
    @PutMapping("/update/status")
    public Result<String> updateStatus(@RequestBody VoucherUpdateStatusDTO requestParam) {
        voucherService.updateStatus(requestParam);
        return Result.success("状态更新成功");
    }

    /**
     * 判断当前优惠券是否上架
     */
    @GetMapping("/isOnline/{id}")
    public Result<Boolean> isOnline(@PathVariable Long id){
        return Result.success(voucherService.isOnline(id));
    }
}